package com.itheima.reggie_auto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie_auto.dto.SetmealDto;
import com.itheima.reggie_auto.entity.Category;
import com.itheima.reggie_auto.entity.Setmeal;
import com.itheima.reggie_auto.entity.SetmealDish;
import com.itheima.reggie_auto.exception.CustomException;
import com.itheima.reggie_auto.mapper.SetmealMapper;
import com.itheima.reggie_auto.service.CategoryService;
import com.itheima.reggie_auto.service.SetmealDishService;
import com.itheima.reggie_auto.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public void save(SetmealDto setmealDto) {
        //分析：本次添加涉及 两张表，一张套餐表 和套餐菜品关系表，  关系表中需要设置套餐id  套餐id在保存套餐后就有值了
        //1.保存套餐信息
        setmealMapper.insert(setmealDto);

        //2.保存套餐菜品关系表的信息
        //2.1先设置套餐的Id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //2.2保存菜品信息到套餐菜品关系表
        setmealDishService.saveSetmealDish(setmealDishes);


    }

    @Override
    public Page<SetmealDto> page(int page, int pageSize, String name) {
        //分析：1.将套餐表信息查出来 ，还差一个分类名称 需要设置进去
        //1.1先定义两个page分页对象
        Page<Setmeal> setmealPage = new Page<>(page, pageSize); //用来从数据库获取数据对象
        Page<SetmealDto> setmealDtoPage = new Page<>();  //用来返回数据对象

        //1.2构造条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);

        //1.3 查询
        setmealMapper.selectPage(setmealPage, lqw);

        //1.4将查询的结果进行数据对拷，除了records
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        //1.5获取records 遍历 为其赋值
        List<Setmeal> records = setmealPage.getRecords();
        //具有分类名的数据集合
        List<SetmealDto> collect = records.stream().map(item -> {
            //创建新的dto对象来接收值
            SetmealDto setmealDto = new SetmealDto();

            //将原有数据进行对拷
            BeanUtils.copyProperties(item, setmealDto);

            //获取分类Id  从分类表中进行查询
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            //设置dto中
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(collect);

        return setmealDtoPage;
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        //分析：删除套餐必须是停售状态， 并且删除套餐需要将套餐关系表的数据也要进行删除
        //停售状态抛异常--根据Id和status=1查询  id是集合 所以where条件用in
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getStatus, 1);
        lqw.in(Setmeal::getId, ids);
        Integer count = setmealMapper.selectCount(lqw);

        if (count > 0) {
            //抛出异常
            throw new CustomException("起售状态不能删除！");
        }

        //先删除套餐表 再删除 关系表
        setmealMapper.deleteBatchIds(ids);

        //删除关系表
        setmealDishService.deleteBySetmealIds(ids);


    }

    @Override
    public void updateStatus(Integer status, Long[] ids) {
        //分析：批量更新状态
        /*LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids);

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealMapper.update(setmeal, lqw);*/
        LambdaUpdateWrapper<Setmeal> luw = new LambdaUpdateWrapper<>();
        luw.in(Setmeal::getId,ids);
        luw.set(status!=null,Setmeal::getStatus,status);
        // Setmeal setmeal = new Setmeal();
        setmealMapper.update(null,luw);

    }

    @Override
    public SetmealDto getById(Long id) {

        SetmealDto setmealDto = new SetmealDto();
        //分析：1.首先从套餐表查出基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //数据对拷
        BeanUtils.copyProperties(setmeal, setmealDto);
        //2. 再 根据套餐id从关系表查出数据
        List<SetmealDish> setmealDishList = setmealDishService.getBySetmealId(id);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    @Override
    public void updateById(SetmealDto setmealDto) {
        //分析：修改 套餐表的数据  删除关系表信息 再添加关系表的信息，关系表中需要设置套餐id
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        setmealMapper.updateById(setmeal);

        //删除关系表
        List<Long> list = new ArrayList<>();
        list.add(setmealDto.getId());
        setmealDishService.deleteBySetmealIds(list);

        //关系数据 添加套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map(item->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveSetmealDish(setmealDishes);

    }

    @Override
    public List<Setmeal> getList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealMapper.selectList(lqw);
        return setmeals;
    }
}
